<?php
namespace addon\mall\controller;
use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use tpfcore\util\TimeUtil;
use think\Db;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class Settlement extends AddonAdminBase
{
    public function index(){
      $data=$search = $this->param;
      $this->assign('search',$search);
      
      $begin='';$end='';$amount_where='';

      $type = empty($data['type'])?"":$data['type'];
      switch ($type) {
        case 'today':
          $begin=TimeUtil::today()[0];
          $end=TimeUtil::today()[1];
          break;
        case 'week':
          $begin=TimeUtil::week()[0];
          $end=TimeUtil::week()[1];
          break;
        case 'month':
          $begin=TimeUtil::month()[0];
          $end=TimeUtil::month()[1];
          break;
        case 'quarter':
          $begin=TimeUtil::quarter()[0];
          $end=TimeUtil::quarter()[1];
          break;
         case 'year':
          $begin=TimeUtil::year()[0];
          $end=TimeUtil::year()[1];
          break;
        default:
          break;
      }

      if(empty($type)){
        !empty($data['start_time']) ? $begin=strtotime($data['start_time']) : '';
        empty($data['end_time']) ? $end=time() : $end=strtotime($data['end_time']);
      }
      if($begin && $end){
        $amount_where="AND pay_time BETWEEN $begin AND $end";
      }
      
      /* 同比上个月 */
      $last_month = TimeUtil::lastMonth();
      $month = TimeUtil::month();
      $sql_month_amount_increase="SELECT 
                sum(
                  CASE
                    WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_time BETWEEN {$last_month[0]} AND {$last_month[1]}
                    THEN order_amount
                    ELSE 0
                    END
                ) as last_month_total_amount,
                sum(
                  CASE
                    WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_time BETWEEN {$month[0]} AND {$month[1]}
                    THEN order_amount
                    ELSE 0
                    END
                ) as month_total_amount
                FROM ".DB_PREFIX."order";
      $result_increase = Db::query($sql_month_amount_increase);

      /* 其它数据统计 */
      $sql="SELECT 
              sum(
                CASE 
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 $amount_where
                  THEN 1
                  ELSE 0
                  END
              ) as order_success_number,
              sum(
                CASE 
                  WHEN pay_status=1 AND order_status=4 $amount_where
                  THEN 1
                  ELSE 0
                  END
              ) as order_refund_number,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as total_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='weixin' AND pay_code='pc' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as wechat_pc_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='weixin' AND pay_code='wap' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as wechat_h5_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='weixin' AND pay_code='mp' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as wechat_mp_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='weixin' AND pay_code='wxapp' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as wechat_wxapp_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='alipay' AND pay_code='pc' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as alipay_pc_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='alipay' AND pay_code='wap' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as alipay_wap_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 AND pay_handle='alipay' AND pay_code='app' $amount_where
                  THEN order_amount
                  ELSE 0
                  END
              ) as alipay_app_amount,
              sum(
                CASE
                  WHEN pay_status=1 AND order_status=3 AND shipping_status=3 $amount_where
                  THEN user_money
                  ELSE 0
                  END
              ) as balance_amount
              FROM ".DB_PREFIX."order";
      $result = Db::query($sql);
      
      $last_month_sql="SELECT lefttable.date,IFNULL(righttable.total,'0') as money,IFNULL(righttable.order_number,0) as order_number,IFNULL(righttable.refund_order_number,'0') as refund_order_number,IFNULL(righttable.refund_total,'0.00') as refund_total
FROM (SELECT date_sub(CURDATE(),interval @i:=@i+1 day) as date 
from (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1) as tmp,(select @i:= -1) t) as lefttable
LEFT JOIN 
(SELECT 
sum(CASE WHEN pay_status=1 AND order_status=3 AND shipping_status=3 THEN order_amount ELSE 0 END) as total,
sum(CASE WHEN pay_status=1 AND order_status=3 AND shipping_status=3 THEN 1 ELSE 0 END) as order_number,
sum(CASE WHEN pay_status=1 AND order_status=4 THEN 1  ELSE 0 END) as refund_order_number,
sum(CASE WHEN pay_status=1 AND order_status=4 THEN order_amount ELSE 0  END) as refund_total,
FROM_UNIXTIME(pay_time,'%Y-%m-%d') as paytime
FROM tpf_order
WHERE pay_time BETWEEN UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) AND UNIX_TIMESTAMP(NOW())
GROUP BY paytime) as righttable
ON lefttable.date=righttable.paytime";
      $list = Db::query($last_month_sql);

    	$this->addonTemplate('index',[
        "list"=>$list,
        "total"=>$result[0],
        "increase"=>$result_increase[0]
    	]);
    }
}
